package com.platform.qujia.service.impl;

import com.platform.qujia.param.response.YunhuaPayRes;
import com.platform.qujia.param.response.YunhuaResponse;
import com.yunhua.oil.common.core.utils.LocalDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.platform.qujia.config.BasicInfo;
import com.platform.qujia.constantEnum.TianQuetechPayEnum;
import com.platform.qujia.dao.TradePaymentRecordsDao;
import com.platform.qujia.param.request.QuestEnterpriseParam;
import com.platform.qujia.param.request.TradeReverseScanPayParameter;
import com.platform.qujia.pay.service.impl.TianQuetechPay;
import com.platform.qujia.pojo.PayWay;
import com.platform.qujia.pojo.SelectPayWayEnterpriseVO;
import com.platform.qujia.pojo.TradePaymentRecordsDO;
import com.platform.qujia.pojo.TradeReversePayInfo;
import com.platform.qujia.pojo.TradeReverseScanPayNotifyDO;
import com.platform.qujia.service.TradePaymentRecordsService;



@Service
public class TradePaymentRecordsServiceImpl implements TradePaymentRecordsService {
	
	private static Logger logger =(Logger) LoggerFactory.getLogger(TianQuetechPay.class);
	
	@Autowired
	private TradePaymentRecordsDao tradePaymentRecordsDao;
	
	@Override
	public TradePaymentRecordsDO get(Integer id){
		return tradePaymentRecordsDao.get(id);
	}
	
	@Override
	public List<TradePaymentRecordsDO> list(Map<String, Object> map){
		return tradePaymentRecordsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return tradePaymentRecordsDao.count(map);
	}
	
	@Override
	public int save(TradePaymentRecordsDO tradePaymentRecords){
		return tradePaymentRecordsDao.save(tradePaymentRecords);
	}
	
	@Override
	public int update(TradePaymentRecordsDO tradePaymentRecords){
		return tradePaymentRecordsDao.update(tradePaymentRecords);
	}
	
	@Override
	public int remove(Integer id){
		return tradePaymentRecordsDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return tradePaymentRecordsDao.batchRemove(ids);
	}
	
	@Override
	public int addTradePaymentRecords(TradeReverseScanPayParameter payParameter,String orderSn) {
		TradePaymentRecordsDO record=new TradePaymentRecordsDO();
		record.setOrderSn(orderSn);
		record.setAddTime(new Date());
		record.setAmt(new BigDecimal(payParameter.getAmt()));
		record.setAuthCode(payParameter.getAuthCode());
		record.setMno(payParameter.getMno());
		record.setOrdNo(payParameter.getOrdNo());
		record.setSubject(payParameter.getSubject());
		record.setTrmip(payParameter.getTrmIp());
		record.setStatus(TianQuetechPayEnum.PAY_PAYING.getValue());
		return tradePaymentRecordsDao.save(record);
	}

	@Override
	public int updateTradePaymentRecords(TradeReverseScanPayNotifyDO notifdo) {
		//TODO 可以添加openid
		if(notifdo.getOrdNo()==null) {
			logger.info("回调更新---订单号为null，不更新支付记录。");
			return 0;
		}
		TradePaymentRecordsDO recordsDO=tradePaymentRecordsDao.selectorderSn(notifdo.getOrdNo());
		if(recordsDO==null) {
			logger.info("回调更新---订单号:"+notifdo.getOrdNo()+"|未查询到订单");
			return 0;
		}
		TradePaymentRecordsDO tradePaymentRecords=new TradePaymentRecordsDO();
		tradePaymentRecords.setId(recordsDO.getId());
		tradePaymentRecords.setStatus(TianQuetechPayEnum.PAY_SUCCESS.getValue());
		tradePaymentRecords.setUuid(notifdo.getUuid());
		tradePaymentRecords.setOrigOrdno(notifdo.getOrigOrdNo());
		tradePaymentRecords.setSettleAmt(new BigDecimal(notifdo.getSettleAmt()==null?"0":notifdo.getSettleAmt()));
		tradePaymentRecords.setRecfeeRate(new BigDecimal(notifdo.getRecFeeRate()==null?"0":notifdo.getRecFeeRate()));
		tradePaymentRecords.setRecfeeAmt(new BigDecimal(notifdo.getRecFeeAmt()==null?"0":notifdo.getRecFeeAmt()));
		//子商户号
		tradePaymentRecords.setSubMechid(notifdo.getSubMechId());
		//落单号 供退款使用。
		tradePaymentRecords.setSxfUuid(notifdo.getSxfUuid());
		//时间格式转换 时间戳转为yyyy-MM-dd HH:mm:ss
		tradePaymentRecords.setPayTime(new Date(Long.parseLong(String.valueOf(notifdo.getPayTime()))));
		return tradePaymentRecordsDao.update(tradePaymentRecords);	
	}

	@Override
	public int updateTradeReversePayInfo(TradeReversePayInfo payInfo) {
		if(payInfo.getOrdNo()==null) {
			logger.info("订单号为null，不更新支付记录。"+payInfo.getBizMsg());
			return 0;
		}
		TradePaymentRecordsDO recordsDO=tradePaymentRecordsDao.selectOneOrdNo(payInfo.getOrdNo());
		if(recordsDO==null) {
			logger.info("订单号:"+payInfo.getOrdNo()+"|未查询到订单");
			return 0;
		}
		if(recordsDO.getStatus()==TianQuetechPayEnum.PAY_REFUNDED.getValue()) {
			logger.info("订单号:"+payInfo.getOrdNo()+"|已退款不进行更新");
			return 0;
		}
		TradePaymentRecordsDO tradePaymentRecords=new TradePaymentRecordsDO();
		tradePaymentRecords.setId(recordsDO.getId());
		if(payInfo.getBizCode().equals("0000")) {
			tradePaymentRecords.setStatus(TianQuetechPayEnum.PAY_SUCCESS.getValue());
			tradePaymentRecords.setUuid(payInfo.getUuid());
			tradePaymentRecords.setSettleAmt(new BigDecimal(payInfo.getSettleAmt()==null?"0":payInfo.getSettleAmt()));
			tradePaymentRecords.setRecfeeRate(new BigDecimal(payInfo.getRecFeeRate()==null?"0":payInfo.getRecFeeRate()));
			tradePaymentRecords.setRecfeeAmt(new BigDecimal(payInfo.getRecFeeAmt()==null?"0":payInfo.getRecFeeAmt()));
			//落单号 供退款使用。
			tradePaymentRecords.setSxfUuid(payInfo.getSxfUuid());
		}
		//时间格式转换 时间戳转为yyyy-MM-dd HH:mm:ss
		tradePaymentRecords.setPayTime(new Date(Long.parseLong(String.valueOf(payInfo.getPayTime()))));
		return tradePaymentRecordsDao.update(tradePaymentRecords);	
	}

	@Override
	public int updateYunHuaPayInfo(YunhuaResponse<YunhuaPayRes> result) {
		YunhuaPayRes res = result.getData();
		String orderNo = res.getClientOrderNo();
		if(orderNo ==null) {
			logger.info("订单号为null，不更新支付记录，msg:{}",res.getErrMsg());
			return 0;
		}
		TradePaymentRecordsDO recordsDO=tradePaymentRecordsDao.selectorderSn(orderNo);
		if(recordsDO==null) {
			logger.info("订单号:{}|未查询到订单", orderNo);
			return 0;
		}
		if(Objects.equals(recordsDO.getStatus(), TianQuetechPayEnum.PAY_REFUNDED.getValue())) {
			logger.info("订单号:{}|已退款不进行更新",orderNo);
			return 0;
		}
		TradePaymentRecordsDO tradePaymentRecords=new TradePaymentRecordsDO();
		tradePaymentRecords.setId(recordsDO.getId());
		if("0000".equals(result.getCode())) {
			tradePaymentRecords.setStatus(TianQuetechPayEnum.PAY_SUCCESS.getValue());
			tradePaymentRecords.setUuid(res.getTransInfoNo());
			//tradePaymentRecords.setSettleAmt(new BigDecimal(res.getSettleAmt()==null?"0":res.getSettleAmt()));
			//tradePaymentRecords.setRecfeeRate(new BigDecimal(res.getRecFeeRate()==null?"0":res.getRecFeeRate()));
			//tradePaymentRecords.setRecfeeAmt(new BigDecimal(res.getRecFeeAmt()==null?"0":res.getRecFeeAmt()));

			//保存云化流水号
			tradePaymentRecords.setSxfUuid(res.getTransPayNo());
		}
		//时间格式转换 时间戳转为yyyy-MM-dd HH:mm:ss
		if (StringUtils.isNotBlank(res.getPayTime())) {
			LocalDateTime payTime = LocalDateTime.parse(res.getPayTime());
			tradePaymentRecords.setPayTime(LocalDateUtils.localDateTimeToDate(payTime));
		}
		return tradePaymentRecordsDao.update(tradePaymentRecords);
	}


	@Override
	public List<PayWay> selectPayWayEnterpriseId(Integer enterpriseId, String mno) {
		return tradePaymentRecordsDao.selectPayWayEnterpriseId(enterpriseId, mno);
	}

	
	@Override
	public SelectPayWayEnterpriseVO selectPayWayEnterpriseId(QuestEnterpriseParam param) {
		//我偷懒了，可优化 使用sql可以查询指定值进行返回，渠道号可以写在支付代码中。
		//由于前端强烈要求返回TRADE_REVERSE_PAY_TEST_ORGID这个值所以就给前端了
		List<PayWay> list=tradePaymentRecordsDao.selectPayWayEnterpriseId(param.getEnterpriseId(),param.getMno());
		if(list==null || list.size()==0) {
			return null;
		}
		SelectPayWayEnterpriseVO vo=new SelectPayWayEnterpriseVO();
		vo.setEnterpriseId(list.get(0).getEnterpriseId());
		vo.setMno(list.get(0).getMno());
		vo.setOrgId(BasicInfo.TRADE_REVERSE_PAY_ORGID);
		return vo;
	}

	@Override
	public TradePaymentRecordsDO selectOneOrdNo(String ordNo) {
		return tradePaymentRecordsDao.selectOneOrdNo(ordNo);
	}

	@Override
	public int updateOrdNoStatus(String ordNo, Integer status) {
		return tradePaymentRecordsDao.updateOrdNoStatus(ordNo, status);
	}

	@Override
	public TradePaymentRecordsDO selectorderSn(String orderSn) {
		return tradePaymentRecordsDao.selectorderSn(orderSn);
	}
	
}
